<?php
/**
 * 店铺控制器
 * @version 1.0
 * @author ezhu <ezhu@jipukeji.com>
 */

namespace Shop\Event;

class StoreEvent{
    
    
    /**
     * 获取店铺首页信息统计和待处理事件
     */
    public function stat($store_id){
        // 店铺提示
        
        $storage = M('Store')->where(array('store_id'=>$store_id))->getField('storage_alarm');
        // 商品总数
        $data['item'] = M('Item')->where(array('status'=>array('in',array(0,1,-2)),'store_id'=>$store_id))->count();
        // 下架商品
        $data['unShelve'] = M('Item')->where('`status` = 0 AND `store_id`="'.$store_id.'"')->count();
        // 违规下架
        $data['violation'] = M('Item')->where('`status` = -2 AND `store_id`="'.$store_id.'"')->count();
        // 库存不足
        $data['lack'] = M('Item')->where('`status` > 0 AND `store_id`="'.$store_id.'" AND `stock` <= "'.$storage.'"')->count();
        // 销售中
        $sell = $data['item'] - $data['unShelve'] - $data['violation'];
        $data['sell'] = $sell >= 0 ? $sell : 0;
        
        $oMap['store_ids'] = $store_id;
        // 交易中
        $oMap['o_status'] = array('in',array(200,201));
        //$data['order'] = M('Order')->where($oMap)->count();
        $data['order'] = M('StoreOrder')->where($oMap)->count();
        // 待支付
        $oMap['o_status'] = array('eq',0);
        //$data['unpay'] = M('Order')->where($oMap)->count();
        $data['unpay'] = M('StoreOrder')->where($oMap)->count();
        // 待发货
        $oMap['o_status'] = array('eq',200);
        //$data['unpost'] = M('Order')->where($oMap)->count();
        $data['unpost'] = M('StoreOrder')->where($oMap)->count();
        // 待收货
        $oMap['o_status'] = array('eq',201);
        //$data['unget'] = M('Order')->where($oMap)->count();
        $data['unget'] = M('StoreOrder')->where($oMap)->count();
        // 待审核退款
        $oMap['o_status'] = array('eq',300);
        //$data['unrefund'] = M('Order')->where($oMap)->count();
        $data['unrefund'] = M('StoreOrder')->where($oMap)->count();
        // 待退货
        //         $oMap['o_status'] = array('eq',301);
        //         $data['reback'] = M('Order')->where($oMap)->count();
        //         // 退款成功
        //         $oMap['o_status'] = array('eq',303);
        //         $data['refundSuccess'] = M('Order')->where($oMap)->count();
        //         // 退款驳回
        //         $oMap['o_status'] = array('eq',405);
        //         $data['norefund'] = M('Order')->where($oMap)->count();
        
        return $data;
    }
    
    
    /**
     * 店铺账户余额
     * @param number $store_id   店铺id
     * @param string $isAll      是否总收入，总收入包含店铺已提现部分
     */
    public function finance($store_id=0,$isAll=false){
        $amount = 0;
        $fee = 0;
        // 店铺收入总金额
        $map['store_id'] = $store_id;
        $map['type'] = 'consume'; //用户消费
        $map['flow'] = 'in';
        $map['status'] = 1;
        $model = M('StoreCash');
        $all = $model->where($map)->sum('amount');
        
        // 用户退款
        $map['type'] = 'refund'; //用户消费
        $map['flow'] = 'out';
        $refund = $model->where($map)->sum('amount');
        
        // 当前账户余额，则减去已提现部分
        if(!$isAll){
            $where['store_id'] = $store_id;
            $where['status'] = array('in',array('1','3','5'));
            $table = M('StoreWithdraw');
            $list = $table->where($where)->field('id,fee,amount,status')->select();
            if(!empty($list)){
                foreach ($list as $key=>$val){
                    $amount += $val['amount'];
                    $fee += $val['fee'];
                }
            }
            $amount = ($amount - $fee) > 0 ? $amount - $fee : 0;
        }
        $all = $all - $refund - $amount;
        return $all > 0 ? $all : 0;
    }
    
    
    /**
     * 获取未读取条数
     * @return 
     */
    public function unReadMsg($store_id=0){
        $mid_lists = M('StoreMessageRecord')->field('message_id')->where(array('store_id' => $store_id))->select();
        $mids = array_column($mid_lists, 'message_id');
        empty($mids) && $mids = array(0);
        $where = $this->getWhereArray($store_id);
        $where['id'] = array('NOT IN', $mids);
        $count = M('StoreMessage')->where($where)->count();
        return $count;
    }
    
    
    /**
     * 获取查询条件
     * @author Max.Yu <max@jipu.com>
     */
    public function getWhereArray($store_id = 0){
        $where = array(
            '_complex' => array(
                    'to_store' => 0,
                    '_string' => ' FIND_IN_SET('.$store_id.', `to_store`)',
                    '_logic' => 'or',
            ),
            'status' => 1
        );
        return $where;
    }
    
    
    /**
     * 获取店铺消息列表
     * @author Max.Yu <max@jipu.com>
     */
    public function msgLists($map = array()){
        $record = M('StoreMessageRecord');
        $store_id = intval($map['store_id']);
        $where = $this->getWhereArray($store_id);
        //已删除数据
        $delete_lists = $record->field('message_id')->where(array('store_id' => $store_id, 'status' => -1))->select();
        $del_ids = array_column($delete_lists, 'message_id');
        //已读未读查询
        if($map['type'] != 'all'){
            $mr_lists = $record->field('message_id')->where(array('store_id' => $store_id, 'status' => 1))->select();
            $mids = array_column($mr_lists, 'message_id');
            empty($mids) && $mids = array(0);
            if($map['type'] == 'unread'){
                $where['id'] = array('NOT IN', array_merge($del_ids, $mids));
            }elseif($map['type'] == 'read'){
                $where['id'] = array('IN', $mids);
            }
            //全部消息
        }else{
            $del_ids && $where['id'] = array('NOT IN', $del_ids);
        }
        $where['title|content'] = $map['title|content'];
        $limit = isset($map['limit']) ? $map['limit'] : 15; 
        $lists = A('Home/Page', 'Event')->lists('StoreMessage', $where, 'id desc', $limit, '', 'id,uid,title,ctime,content,to_store, status');
        foreach($lists as &$li){
            $r_line = $record->where(array('store_id' => $store_id, 'message_id' => $li['id']))->find();
            $li['is_read'] = $r_line ? 1 : 0;
        }
        return $lists;
    }
    
    /**
     * 获取店铺中商品ID（排除参加其他活动）
     * @author buddha <[email address]>
     * @param  [type] $store_id 当前店铺ID 
     * @return [type] [description]
     */
    public function getItemIds( $store_id = 0 ){
        //老客户
        $map = array(
                'store_id' => $store_id,
                'status'   => 1
            );
        $fugou = M('Fugou')->where($map)->getField('item_id',true);
        $fugou_items = arr2str($fugou);
        //第二件折扣
        $map = array(
                'expire_time' => array('gt',time())
            );
        $second_pieces = M('SecondPieces')->where($map)->getField('item_id',true);
        $second_items = arr2str($second_pieces);
        //秒杀
        $seckill = M('Seckill')->where($map)->getField('item_ids',true);
        $seckill_items = arr2str($second_pieces);

        $item_ids = $fugou_items.','.$second_items.','.$seckill_items;

        return $item_ids;
    }
}
